package aip.assignment2.persistence.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Stone
 */
@Entity
@Table(name = "purchase_product")
@XmlRootElement
@NamedQueries(
{
	@NamedQuery(name = "PurchaseProduct.findAll", query = "SELECT p FROM PurchaseProduct p"),
	@NamedQuery(name = "PurchaseProduct.findById", query = "SELECT p FROM PurchaseProduct p WHERE p.id = :id"),
	@NamedQuery(name = "PurchaseProduct.findByQuantity", query = "SELECT p FROM PurchaseProduct p WHERE p.quantity = :quantity")
})
public class PurchaseProduct implements Serializable
{
	private static final long serialVersionUID = 1L;
	@Id
	@SequenceGenerator(name = "purchase_product_id", allocationSize = 1, sequenceName = "purchase_product_id_seq")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "purchase_product_id")
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	@Basic(optional = false)
	@NotNull
	@Column(name = "quantity")
	private int quantity;
	@JoinColumn(name = "purchase", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private Purchase purchase;
	@JoinColumn(name = "product", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private Product product;
	public PurchaseProduct()
	{
	}
	public PurchaseProduct(Integer id)
	{
		this.id = id;
	}
	public PurchaseProduct(Integer id, int quantity)
	{
		this.id = id;
		this.quantity = quantity;
	}
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public int getQuantity()
	{
		return quantity;
	}
	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}
	public Purchase getPurchase()
	{
		return purchase;
	}
	public void setPurchase(Purchase purchase)
	{
		this.purchase = purchase;
	}
	public Product getProduct()
	{
		return product;
	}
	public void setProduct(Product product)
	{
		this.product = product;
	}
	public BigDecimal getPrice()
	{
		return product.getPrice().multiply(new BigDecimal(quantity));
	}
	@Override
	public int hashCode()
	{
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}
	@Override
	public boolean equals(Object object)
	{
		// TODO: Warning - this method won't work in the case the id fields are not set
		if (!(object instanceof PurchaseProduct))
		{
			return false;
		}
		PurchaseProduct other = (PurchaseProduct) object;
		if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
		{
			return false;
		}
		return true;
	}
	@Override
	public String toString()
	{
		return "aip.assignment2.data.entity.PurchaseProduct[ id=" + id + " ]";
	}
}
